﻿import com.yourdomain.spreadshop.gui.GUI;
import com.yourdomain.spreadshop.gui.mod.lib.Module;
import com.yourdomain.spreadshop.gui.mod.lib.ScrollBar;

import net.spreadshirt.logger.Logger;

/**
 * The TextBox class displays a simple html formatted text.
 */
class com.yourdomain.spreadshop.gui.mod.TextBox {

	// The reference to the swf file.
	private static var _SWF:String = "spreadshop_textBox.swf";

	// Various visual settings.
	private static var _POSITION:Object = { x: 8, y: 8 };
	private static var _LABEL_TITLE:String = "Info";
	private static var _LABEL_OKAY:String = "OK";

	// The reference to the GUI main class.
	private var _gui:GUI;

	// The movie clip container.
	private var _clip:MovieClip;

	/**
	 * TextBox Constructor
	 */
	public function TextBox(
		gui:GUI,
		clip:MovieClip,
		level:Number
	) {
		// Connects with the GUI instance.
		_gui = gui;
		// Create a new movie clip and set it's position on stage.
		_clip = clip.createEmptyMovieClip("textBox", level);
		// Create a listener object to watch loading in progress.
		var mclListener:Object = new Object();
		mclListener.timer = [0, 0];
		mclListener.onLoadStart = function(target:MovieClip):Void {
		    this.timer[0] = getTimer();
			Logger.getLog().info("GUI - TextBox: start loading");
		};
		mclListener.onLoadComplete = function(target:MovieClip):Void {
			this.timer[1] = getTimer();
		};
		mclListener.textBox = this;
		mclListener.onLoadInit = function(target:MovieClip):Void {
			var timer:Number = this.timer[1] - this.timer[0];
			var bytes:Number = target.getBytesLoaded();
			Logger.getLog().info("GUI - TextBox: "
				+ bytes +" Bytes loaded in "+ timer +" ms.");
			this.textBox.setup();
		};
		// Create movie clip loader an attach the listener object.
		var mcLoader:MovieClipLoader = new MovieClipLoader();
		mcLoader.addListener(mclListener);
		mcLoader.loadClip(_gui.swf_path + _SWF, _clip);
	}

	/**
	 * Hides the module
	 */
	public function hide():Void {
		_clip.textBox.hide();
	}

	/**
	 * Setup
	 */
	private function setup():Void {
		_gui.initModuleDone("initTextBox");
		// Creates an alias to the item detail movie clip.
		var clip:MovieClip = _clip.textBox;
		// Setup modul.
		var module:Module = new Module(
			_gui,
			clip,
			false,
			_LABEL_TITLE,
			_POSITION.x,
			_POSITION.y
		);
		// Setup the order button to add the item to the basket.
		var btokay:MovieClip = clip.btokay;
		btokay.tabEnabled = false;
		btokay.label = _LABEL_OKAY;
		btokay.onPress = function():Void {
			this._parent.btclose.onPress();
		};
		// Reset text box.
		clip.reset = function():Void {
			this.textarea.container.tf.htmlText = "";
			this.textarea.container._y = 0;
		};
	}

	/**
	 * Shows the text.
	 */
	 public function showText(text:String):Void {
		// Create an alias to the text box movie clip.
		var clip:MovieClip = _clip.textBox;
		// Reset on close.
		clip.reset();
		clip.onEnterFrame = function():Void {
			if(!this._visible) {
				this.reset();
				delete this.onEnterFrame;
			}
		};
		// Set the position to the center of the stage.
		clip._x = ((Stage.width - clip._width) * .5) >> 0;
		clip._y = ((Stage.height - clip._height) * .4) >> 0;
	 	// Show the item detail movie clip.
	 	clip.setToFront();
	 	// Put text to text area.
	 	var textarea:MovieClip = clip.textarea.container;
		textarea.tf.autoSize = "left";
		//textarea.tf.condenseWhite = true;
		text = text.split("<h1>").join("<font size=\"15\" "
			+"color=\"#666666\"><b>");
		text = text.split("</h1>").join("</b></font><br><br>");
		text = text.split("<h2>").join("<font size=\"13\" "
			+"color=\"#666666\"><b>");
		text = text.split("</h2>").join("</b></font><br>");
		text = text.split("<br />").join("<br>");
		text = text.split("<strong>").join("<b>");
		text = text.split("</strong>").join("</b>");
		text = text.split("<div style=\"text-decoration: underline; display: inline\">").join("<p>___________________________________</p>");
		text = text.split('<a ').join('<font color="#0099ff"><u><a target="blank" ').split('</a>').join('</a></u></font>');
		
	 	textarea.tf.htmlText = text;/**/
		trace(text);
		textarea.cacheAsBitmap = true;
		// Setup scrollbar.
		var isVisible:Boolean =  (textarea._height > 270);//### JNF ### textarea.container._height should have be textarea.tf._height or just textarea._height
		var scrollbar:ScrollBar = new ScrollBar(
			_gui,
			clip.scrollbar,
			textarea,
			isVisible
		);
	 }

}
